package com.lsnu.wzy.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lsnu.wzy.po.Administrator;
import com.lsnu.wzy.po.Appointment;
import com.lsnu.wzy.po.Doctor;
import com.lsnu.wzy.po.EchartsData;
import com.lsnu.wzy.po.Patient;
import com.lsnu.wzy.service.AppointmentService;
import com.lsnu.wzy.service.DoctorService;

@RestController
@RequestMapping("/appointOption")
public class AppointmentController {
	@Autowired
	private AppointmentService appointmentService;

	
	//搜索预约
	@RequestMapping("/findById/{dID}")
	public ModelAndView FindById(String aID)
	{
		List<Appointment> list = new ArrayList<Appointment>();
		Appointment appointment= appointmentService.findAppointment(aID);
		list.add(appointment);
		ModelAndView mav=new ModelAndView("/jsp/pages/appointments-list.jsp");
		mav.addObject("appointment", list);
		mav.addObject("aID",aID);
		return mav;		
	}
		
	//患者根据姓名搜索预约
	@RequestMapping("/findByPname")
	public void FindByPname(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException, NumberFormatException 
	{
		HttpSession session = request.getSession();
		String pName=((Patient) request.getSession().getAttribute("patient")).getpName();
		System.out.println(pName);
		List<Appointment> list = appointmentService.findPatientAppointment(pName);
		System.out.println(list);
		session.setAttribute("appointment", list);
		request.getRequestDispatcher("/jsp/index3.jsp").forward(request,response); 
	}
	
	//管理员点击添加事件
	@RequestMapping("/beforeADD")
	public ModelAndView beforeADD()
	{
		Appointment appointment=appointmentService.findLast();
		String aID=appointment.getaID();
		char[] arr=aID.toCharArray();
		StringBuilder builder = new StringBuilder();
	        for (int i = 0; i < arr.length; i++) {
//	        	如果字符的ACSLL码在48-57的范围内那肯定就是整数了
				if(arr[i] >= 48 && arr[i] <= 57) {
					builder.append(arr[i]);
				}
			}
	    Integer integer=(Integer.parseInt(builder.toString()))+1;
		String aString="A00"+integer;
		ModelAndView mav=new ModelAndView("/appointOption/addListen");
		mav.addObject("aID",aString);
		return mav;
		}
		
	//管理员添加预约
	@RequestMapping("/addAppointment")
	public void ADDAppointment(HttpServletRequest request,HttpServletResponse response,Appointment appointment) throws ServletException, IOException 
	{
		/*
		 * String aID= request.getParameter("aID"); String pName=
		 * request.getParameter("pName"); Date pDate = null; // String 类型按照 yyyy-MM-dd
		 * 的格式转换为 java.util.Date 类 SimpleDateFormat simpleDateFormat = new
		 * SimpleDateFormat("yyyy-MM-dd"); try { pDate =
		 * simpleDateFormat.parse(request.getParameter("pDate")); } catch
		 * (ParseException e) { e.printStackTrace(); } String dName=
		 * request.getParameter("dName"); String department=
		 * request.getParameter("department"); appointment.setaID(aID);
		 * appointment.setdName(dName); appointment.setpDate(pDate);
		 * appointment.setdName(dName); appointment.setDepartment(department);
		 * System.out.println(appointment);
		 */
		appointmentService.addAppointment(appointment);
		request.getRequestDispatcher("/appointOption/lsAppointment").forward(request,response);
	}
		
	//患者添加预约
	@RequestMapping("/addAppointment2")
	public void ADDAppoint(HttpServletRequest request,HttpServletResponse response,Appointment appointment) throws ServletException, IOException 
	{
		appointmentService.addAppointment(appointment);
		request.getRequestDispatcher("/doctorOption/lsDoctor2").forward(request,response);
	}
		
	//删除预约
	@RequestMapping("/delAppointment/{id}")
	public void DELAppointment(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer id) throws ServletException, IOException 
	{
		appointmentService.delAppointment(id);
		request.getRequestDispatcher("/appointOption/lsAppointment").forward(request,response); 
	}
		
	//编辑前的信息
	@RequestMapping("/editfind/{aID}")
	public ModelAndView FindById2(@PathVariable String aID)
	{
		List<Appointment> list = new ArrayList<Appointment>();
		Appointment appointment= appointmentService.findAppointment(aID);
		list.add(appointment);
		List<String> department= appointmentService.LSDeName();
		List<String> doctor= appointmentService.LSDoctor();
		List<String> patient= appointmentService.LSPatient();
		ModelAndView mav=new ModelAndView("/jsp/pages/editAppointment.jsp");
		mav.addObject("appointment", list);
		mav.addObject("aID",aID);
		mav.addObject("department",department);
		mav.addObject("doctor",doctor);
		mav.addObject("patient",patient);
		return mav;		
	}
		
	//编辑预约信息
	@RequestMapping("/editAppointment/{id}")
	public void EditAppointment(HttpServletRequest request,HttpServletResponse response, Appointment appointment) throws ServletException, IOException 
	{	
		appointmentService.editAppointment(appointment);
		request.getRequestDispatcher("/appointOption/lsAppointment").forward(request,response); 
	}
		 
	//全部预约
	@RequestMapping("/lsAppointment")
	public ModelAndView LSAppointment()
	{
		List<Appointment> appointmentlist= appointmentService.LSAppointment();
		ModelAndView mav=new ModelAndView();
		mav.addObject("appointment",appointmentlist);
		mav.setViewName("/jsp/pages/appointments-list.jsp");
		return mav;		

	}
		
		
	/*
	 * //预约统计
	 * 
	 * @RequestMapping("/LSDepartment")
	 * 
	 * @ResponseBody public List<EchartsData> LSDepartment() {
	 * System.out.println("调用！！"); List<EchartsData> departmentlist=
	 * appointmentService.LSDepartment(); return departmentlist; }
	 */
	@RequestMapping("/LSDepartment")
	public ModelAndView LSDepartment() 
	{
		System.out.println("调用！！"); 
		List<EchartsData> departmentlist=appointmentService.LSDepartment(); 
		System.out.println(departmentlist);
		ModelAndView mav=new ModelAndView();
		mav.addObject("department",departmentlist);
		mav.setViewName("/jsp/pages/tu.jsp");
		return mav;
	}
		
	//下拉框
	@RequestMapping("/addListen")
	public ModelAndView addListen()
	{
		List<String> department= appointmentService.LSDeName();
		List<String> doctor= appointmentService.LSDoctor();
		List<String> patient= appointmentService.LSPatient();
		ModelAndView mav=new ModelAndView();
		mav.addObject("department",department);
		mav.addObject("doctor",doctor);
		mav.addObject("patient",patient);
		//System.out.println(department);
		mav.setViewName("/jsp/pages/addAppointment.jsp");
		return mav;		
	}
		
	//患者添加预约下拉框
	@RequestMapping("/addListen2/{dID}/{dateTime}")
	public ModelAndView patientAddListen(@PathVariable String dID,@PathVariable String dateTime)
	{
		System.out.println(dID);
		Appointment appointment=appointmentService.findLast();
		String aID=appointment.getaID();
		char[] arr=aID.toCharArray();
		StringBuilder builder = new StringBuilder();
	        for (int i = 0; i < arr.length; i++) {
//	        	如果字符的ACSLL码在48-57的范围内那肯定就是整数了
				if(arr[i] >= 48 && arr[i] <= 57) {
					builder.append(arr[i]);
				}
			}
	    Integer integer=(Integer.parseInt(builder.toString()))+1;
		String aString="A00"+integer;
		System.out.println(aString);
		String doctor= appointmentService.findDoctor(dID);
		String department=appointmentService.findDepartment(dID);
		List<String> patient= appointmentService.LSPatient();
		ModelAndView mav=new ModelAndView();
		mav.addObject("aID",aString);
		mav.addObject("department",department);
		mav.addObject("dName",doctor);
		mav.addObject("dateTime",dateTime);
		mav.addObject("patient",patient);
		//System.out.println(department);
		mav.setViewName("/jsp/pages/patientAddAppointment.jsp");//跳转指定界面
		return mav;		
	}
	/*ModelAndView
	 * 使用ModelAndView类用来存储处理完后的结果数据，以及显示该数据的视图。
	 * 业务处理器调用模型层处理完用户请求后，把结果数据存储在该类的model属性中，把要返回的视图信息存储在该类的view属性中，然后让该ModelAndView返回该Spring MVC框架。框架通过调用配置文件中定义的视图解析器，对该对象进行解析，最后把结果数据显示在指定的页面上。.
	 * 具体作用:
	 * 1、返回指定页面
	 * ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面，
	 * 2、返回所需数值
	 * 使用addObject()设置需要返回的值，addObject()有几个不同参数的方法，可以默认和指定返回对象的名字。
	*/
	
	
	//改变预约状态
	@RequestMapping("/apply/{aID}")
	public ModelAndView apply(@PathVariable String aID)
	{
		appointmentService.updateState(aID);
		ModelAndView mav=new ModelAndView();
		List<Appointment> appointmentlist= appointmentService.LSAppointment();
		mav.addObject("appointment",appointmentlist);
		mav.setViewName("/jsp/pages/appointments-list.jsp");
		return mav;		
	}
	
	//统计
	@RequestMapping("/statistics")
	@ResponseBody
	public List<EchartsData> statistics(HttpServletRequest request,HttpServletResponse response, Appointment appointment) throws ServletException, IOException 
	{
		List<EchartsData> result = appointmentService.statistics();
	
		return result;
	}			

}
